package com.wltt.product.frfront.service;

import com.wltt.product.frfront.config.exception.BusinessException;
import com.wltt.product.frfront.mapper.FormulaMapper;
import com.wltt.product.frfront.model.ResultData;
import com.wltt.product.frfront.model.bo.AllFormulaBO;
import com.wltt.product.frfront.model.bo.FormulaDetailBO;
import com.wltt.product.frfront.model.po.FormulaPO;
import com.wltt.product.frfront.model.vo.*;
import com.wltt.product.frfront.service.base.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @ClassName: FormulaServiceImpl
 * @Description: 配方 逻辑服务类<br/>
 * 不做业务校验，参数校验统一放到前置的Service中
 * @Date: 2025-07-03 18:01:51
 * @Author yanggl
 */
@Service(value = "formulaService")
public class FormulaServiceImpl extends BaseServiceImpl<FormulaPO, Long> implements FormulaService {

    private FormulaMapper formulaMapper;

    //构造方法
    public FormulaServiceImpl(FormulaMapper formulaMapper) {
        super(formulaMapper);
        this.formulaMapper = formulaMapper;
    }

    /**
     * 查询所有配方
     *
     * @return
     */
    @Override
    public ResultData allFormula() {
        List<AllFormulaBO> boList = formulaMapper.queryALL();
        List<AllFormulaRSP> rsps = Optional.ofNullable(boList).orElse(new ArrayList<>())
                .stream().map(bo -> {
                    AllFormulaRSP rsp = new AllFormulaRSP();
                    BeanUtils.copyProperties(bo, rsp);
                    return rsp;
                }).collect(Collectors.toList());
        return ResultData.success(rsps);
    }

    /**
     * 查询所有配方用于下拉选
     *
     * @return
     */
    @Override
    public ResultData allFormulaForSelect() {
        List<FormulaPO> formulaPOList = formulaMapper.allFormulaForSelect();
        List<AllFormulaForSelectRSP> rsps = Optional.ofNullable(formulaPOList).orElse(new ArrayList<>())
                .stream().map(po -> {
                    AllFormulaForSelectRSP rsp = new AllFormulaForSelectRSP();
                    rsp.setId(po.getId());
                    rsp.setFormulaName(po.getFormulaName());
                    return rsp;
                }).collect(Collectors.toList());
        return ResultData.success(rsps);
    }

    /**
     * 配方详情
     *
     * @param formulaDetailREQ 配方id
     *
     * @return
     */
    @Override
    public ResultData formulaDetail(FormulaDetailREQ formulaDetailREQ) {

        if (null == formulaDetailREQ || null == formulaDetailREQ.getId()) {
            throw new BusinessException("未指配方", HttpStatus.BAD_REQUEST);
        }

        FormulaDetailBO formulaDetailBO = formulaMapper.formulaDetail(formulaDetailREQ.getId());
        if (null == formulaDetailBO) {
            throw new BusinessException("配方不存在", HttpStatus.BAD_REQUEST);
        }

        FormulaDetailRSP rsp = new FormulaDetailRSP();
        BeanUtils.copyProperties(formulaDetailBO, rsp);
        return ResultData.success(rsp);
    }

    /**
     * 更新配方
     *
     * @param updateFormulaREQ
     * @return
     */
    @Transactional
    @Override
    public ResultData updateFormula(UpdateFormulaREQ updateFormulaREQ) {
        if (null == updateFormulaREQ || null == updateFormulaREQ.getId()) {
            throw new BusinessException("未指配方", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getFormulaName()) {
            throw new BusinessException("未指定配方名称", HttpStatus.BAD_REQUEST);
        }
        updateFormulaREQ.setFormulaName(updateFormulaREQ.getFormulaName().trim());
        if (!StringUtils.hasLength(updateFormulaREQ.getFormulaName())) {
            throw new BusinessException("未指定配方名称", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getWeldingCurrent() || updateFormulaREQ.getWeldingCurrent() < 0) {
            throw new BusinessException("焊接电流无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getWeldingVoltage() || updateFormulaREQ.getWeldingVoltage() < 0) {
            throw new BusinessException("焊接电压无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getWeldingSpeed() || updateFormulaREQ.getWeldingSpeed() < 0) {
            throw new BusinessException("焊接速度无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getSwingTypeId()) {
            throw new BusinessException("焊接类型无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getSwingFrequency() || updateFormulaREQ.getSwingFrequency() < 0) {
            throw new BusinessException("摆动频率无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getSwingRange() || updateFormulaREQ.getSwingRange() < 0) {
            throw new BusinessException("摆动幅度无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getSwingLeftRemainTime() || updateFormulaREQ.getSwingLeftRemainTime() < 0) {
            throw new BusinessException("摆动左停留时间无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getSwingRightRemainTime() || updateFormulaREQ.getSwingRightRemainTime() < 0) {
            throw new BusinessException("摆动右停留时间无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getWaitModeId()) {
            throw new BusinessException("等待模式无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getVerticalTriangleSwingLeftLength() || updateFormulaREQ.getVerticalTriangleSwingLeftLength() < 0) {
            throw new BusinessException("垂直三角摆动左舷长度无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getVerticalTriangleSwingRightLength() || updateFormulaREQ.getVerticalTriangleSwingRightLength() < 0) {
            throw new BusinessException("垂直三角摆动右舷长度无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getVerticalTriangleSwingDotRemainTime() || updateFormulaREQ.getVerticalTriangleSwingDotRemainTime() < 0) {
            throw new BusinessException("垂直三角摆动垂三角点停留时间无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getCircleSwingPullbackRatio() || updateFormulaREQ.getCircleSwingPullbackRatio() < 0 || updateFormulaREQ.getCircleSwingPullbackRatio() > 100) {
            throw new BusinessException("圆形摆动-回调比率无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getSwingLocationWaitId()) {
            throw new BusinessException("摆动位置等待无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateFormulaREQ.getSwingDirectionAngle() || updateFormulaREQ.getSwingDirectionAngle() < 0) {
            throw new BusinessException("摆动方向位角无效", HttpStatus.BAD_REQUEST);
        }

        FormulaPO formulaPO = new FormulaPO();
        BeanUtils.copyProperties(updateFormulaREQ, formulaPO);
        formulaMapper.updateById(formulaPO);
        return ResultData.success();
    }
}